//
//  VisitorView.swift
//  weiboSwift
//
//  Created by 龚欣 on 15/6/24.
//  Copyright © 2015年 gong. All rights reserved.
//

import UIKit
import FFAutoLayout
protocol VisitorViewDelegate : NSObjectProtocol{

    func visitorViewRegistorButtonClick()
    func visitorViewLoginButtonClick()
    
}

class VisitorView: UIView {

   weak var delegate : VisitorViewDelegate?
    
   override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

   required init(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    func setUpUI(){
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registorBtn)
        addSubview(loginBtn)
        

        iconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -80))
        homeView.ff_AlignInner(ff_AlignType.CenterCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 0))
        messageLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPointZero)
        registorBtn.ff_AlignVertical(ff_AlignType.BottomLeft, referView: messageLabel, size: CGSizeMake(100, 40), offset: CGPoint(x: 0, y: 20))
        loginBtn.ff_AlignVertical(ff_AlignType.BottomRight, referView: messageLabel, size: CGSizeMake(100, 40), offset: CGPoint(x: 0, y: 20))
        
        maskIconView.ff_Fill(self, insets: UIEdgeInsetsMake(0, 0, 240, 0))
        
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
    }
    
    func setViewInfo(iconImageName : String, message : String, isHome : Bool){
        if isHome{
        //如果是home页面就显示默认的设置
            
        }else{
            iconView.image = UIImage(named: iconImageName)
            messageLabel.text = message
            homeView.hidden = true
            sendSubviewToBack(maskIconView)
        }
    }

    func startAnimation(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.duration = 20.0
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        iconView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    lazy var iconView : UIImageView = {
            let iconV = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
            return iconV
        }()
    
    lazy var maskIconView : UIImageView = {
            let maskIconV = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
            
            return maskIconV
        }()
    
    lazy var homeView : UIImageView = {
            let homeV = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
            return homeV
        }()
    
    lazy var messageLabel : UILabel = {
            let messageL = UILabel()
            messageL.text = "关注一些人，回这里看看有什么惊喜"
            messageL.preferredMaxLayoutWidth = 240
            messageL.numberOfLines = 2
            messageL.font = UIFont.systemFontOfSize(14)
            messageL.textColor = UIColor.grayColor()
            messageL.textAlignment = NSTextAlignment.Center
            messageL.sizeToFit()
            return messageL
        }()
    
    lazy var registorBtn : UIButton = {
            let registorB = UIButton()
            registorB.setTitle("注册", forState: UIControlState.Normal)
            registorB.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
            registorB.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
            registorB.addTarget(self, action: "registorButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
            return registorB
        }()
    
    lazy var loginBtn : UIButton = {
            let loginB = UIButton()
            loginB.setTitle("登陆", forState: UIControlState.Normal)
            loginB.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
            loginB.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
            loginB.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
            return loginB
        }()
    
    func registorButtonClick(){
        delegate?.visitorViewRegistorButtonClick()
    }
    
    func loginButtonClick(){
        delegate?.visitorViewLoginButtonClick()
    }
}
